﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineCooperation.Entity;
using OnlineCooperation.DB;

namespace OnlineCooperation
{
    public partial class TeamPost : System.Web.UI.Page
    {
        public static List<Post> PagePost;
        public int posts_per_page = 4;
        public static PostDB PDB;
        public Team CurrentTeam;

        protected void Page_Load(object sender, EventArgs e)
        {
            CurrentTeam = new Team();
            //Session["TeamID"]
            CurrentTeam.ID = int.Parse(Session["TeamID"].ToString());
            //第一次载入
            if (!IsPostBack)
            {
                PDB = new PostDB(CurrentTeam, posts_per_page);
                PagePost = PDB.GetFirstPagePost();
                if (PDB.AllPost.Count>posts_per_page)
                    PagePost = PDB.GetLastPagePost();


            }

         /*   if (IsPostBack)
            {
                string a1 = Request.Form["__EVENTTARGET"];
                if (a1 == "reply_click")
                {
                    string a2 = Request.Form["__EVENTARGUMENT"];

                    int id = int.Parse(a2);


                }
            }*/
        }

        protected void Button_Next_Click(object sender, EventArgs e)
        {
            List<Post> PagePost2 = PDB.GetPrePagePost();
            if (PagePost2 != null)
                PagePost = PagePost2;
            else
                Response.Write(@"<script type=""text/javascript"">alert(""没有下一页了 (╯﹏╰）"")</script>");
        }

        protected void Button_Pre_Click(object sender, EventArgs e)
        {
            List<Post> PagePost2 = PDB.GetNextPagePost();
            if (PagePost2 != null)
                PagePost = PagePost2;
            else
                Response.Write(@"<script type=""text/javascript"">alert(""没有前一页了 (╯﹏╰）"")</script>");
        }

        protected void Button_Last_Click(object sender, EventArgs e)
        {
            List<Post> PagePost2 = PDB.GetFirstPagePost();
            if (PagePost2 != null)
                PagePost = PagePost2;
            else
                Response.Write(@"<script type=""text/javascript"">alert(""已是最后一页%>_<%"")</script>");
        }

        protected void Button_First_Click(object sender, EventArgs e)
        {
            List<Post> PagePost2 = PDB.GetLastPagePost();
            if (PagePost2 != null)
                PagePost = PagePost2;
            else
                Response.Write(@"<script type=""text/javascript"">alert(""已是第一页%>_<%"")</script>");
        }

        protected void Button_Reply_Click(object sender, EventArgs e)
        {
            String IDstr = ((Control)sender).ID;
            /*String EnterReply = EnterYourReply.Value;*/
//             String EnterReply = "擦:滚粗";
            //暂时不考虑topic，且这里涉及CurrentTeam.ID
            /*Post MyReply = new Post(Convert.ToInt32(IDstr.Substring(0,IDstr.IndexOf('B'))), 1, EnterReply, 0, CurrentTeam.ID, DateTime.Now);
            PDB.Insert(MyReply);*/
        }

        protected void BtnCallback_Click(object sender, EventArgs e)
        {
            /*
            string text = Session["UserName"].ToString() + ":" + PostContext.Value;
            string id = PostID.Value;
            int tid = int.Parse(Session["TeamID"].ToString());
            TeamDB td = new TeamDB();
            Team t = td.GetTeamByID(tid);
            Post p = new Post(PagePost[int.Parse(id)].PostID, 1, text, 0, tid, DateTime.Now);
            PDB.Insert(p);
            */
        }

        protected void NewPost_Click(object sender, EventArgs e)
        {
            int tid = int.Parse(Session["TeamID"].ToString());
            string text = Session["UserName"].ToString()+":"+Request.Form["TxtNewPost"];
            TeamDB td = new TeamDB();
            Team t = td.GetTeamByID(tid);
            Post p = new Post(0, 0, text, 0, tid, DateTime.Now);
            List<Post> PagePost2 = PDB.GetNextPagePost();
            
            PDB.Insert(p);
            PDB.AllPost.Add(p);
            PagePost = PDB.GetFirstPagePost();
            PagePost = PDB.GetLastPagePost();
        }


    }
}